//
//  MRTruthDareViewController.m
//  MoRan
//
//  Created by MoRan on 2024/1/20.
//  Copyright © 2024 MoRan. All rights reserved.
//

#import "MRTruthDareViewController.h"

#import "MRTruthDareContentTextCell.h"
#import "MRTruthDareTitleCell.h"
#import "MRTruthDareButtonCell.h"

#import "MRTurntableDetailViewController.h"

@interface MRTruthDareViewController ()

@property (nonatomic,copy) NSString *content;

@end

@implementation MRTruthDareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationBar.backgroundColor = [UIColor clearColor];
    [self setNavigationItemTitleViewWithTitle:@"真心话大冒险"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MRTruthDareContentTextCell" bundle:nil] forCellReuseIdentifier:@"MRTruthDareContentTextCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MRTruthDareTitleCell" bundle:nil] forCellReuseIdentifier:@"MRTruthDareTitleCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MRTruthDareButtonCell" bundle:nil] forCellReuseIdentifier:@"MRTruthDareButtonCell"];
    
    self.tableView.frame = CGRectMake(0,self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight);
}

#pragma mark - cellDataSource

- (NSArray *)cellDataSource {
    
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;
                
        subarr = [NSMutableArray arrayWithCapacity:1];
        
        dict =  @{@"class":MRTruthDareTitleCell.class,
                  @"height":@([MRTruthDareTitleCell getCellFrame:[NSNumber numberWithFloat:84]])};
        [subarr addObject:dict];
        
        dict =  @{@"class":MRTruthDareContentTextCell.class,
                  @"height":@([MRTruthDareContentTextCell getCellFrame:[NSNumber numberWithFloat:450]]),
                  @"data":[MRTruthDareContentTextCellModel ittemModelWithTitle:@"" content:_content tip:@"例如：\n嘟嘟" max:@"500"],
                  @"delegate":@YES};
        [subarr addObject:dict];
        
        dict =  @{@"class":MRTruthDareButtonCell.class,
                  @"height":@([MRTruthDareButtonCell getCellFrame:[NSNumber numberWithFloat:200]]),
                  @"data":[MRTruthDareButtonCellModel ittemModelWithTitle:@"完成"],
                  @"delegate":@YES};
        [subarr addObject:dict];
   
        [arr addObject:subarr];

        self.dataSource = arr;
    }
    return self.dataSource;
}

#pragma mark MRTruthDareContentTextCellDelegate 输入框

- (void)truthDareContentTextCell:(MRTruthDareContentTextCell *)cell textView:(UITextView *)textView {
    _content = textView.text;
}

#pragma mark MRTruthDareButtonCellDelegate 保存

- (void)truthDareButtonCell:(MRTruthDareButtonCell *)cell button:(UIButton *)sender {
    NSArray *dataList = [_content componentsSeparatedByString:@"\n"];
    if (dataList.count >= 2) {
        [self gotoTurntableDetailViewController:dataList];
    } else {
        [self showProgressHUD:@"请至少输入两位玩家"];
    }
}

#pragma mark - goto

- (void)gotoTurntableDetailViewController:(NSArray *)dataList {
    MRTurntableDetailViewController* vc = [MRTurntableDetailViewController new];
    vc.dataList = dataList;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
